package com.tdkj.action.front.information;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanInfoComment;
import com.tdkj.model.information.BeanInfoReply;
import com.tdkj.model.information.BeanModuleColumn;
import com.tdkj.model.user.BeanMessage;
import com.tdkj.model.user.BeanUser;
import com.tdkj.model.website.BeanKeywordsFilter;
import com.tdkj.service.common.IBeanADService;
import com.tdkj.service.information.IBeanBaseInfoService;
import com.tdkj.util.PublicConfing;

public class BaseInfoAction extends BaseAction {
	private Long id;
	private Long userId;
	private Long commentid;
	private String msgtitle;
	private String msgcontent;
	private BeanBaseInfo bean;
	private String title;
	private String moduletype;
	private String modulename;
	private String columntype;
	private String columnname;
	private int clicktime;
	private BeanUser user;
	private Timestamp createtime;
	private String resource;
	private String content;
	private String comment_content;
	private String reply_content;
	private Map<String, String> prop;
	private Map<String, String> adMap = new HashMap<String, String>();
	private Set<BeanInfoComment> articleComments = new HashSet<BeanInfoComment>(0);
	private IBeanBaseInfoService beanBaseInfoService;
	private IBeanADService adService;
	private Map<String, List<BeanBaseInfo>> info = new HashMap<String, List<BeanBaseInfo>>();
	private List<BeanBaseInfo> infolist = new ArrayList<BeanBaseInfo>();
	private List<BeanBaseInfo> hotinfolist = new ArrayList<BeanBaseInfo>();// 热门信息
	private List<BeanBaseInfo> relevanceinfolist = new ArrayList<BeanBaseInfo>();// 相关信息

	private String validateText;
    private String repaylText;
    
	public String getRepaylText() {
		return repaylText;
	}

	public void setRepaylText(String repaylText) {
		this.repaylText = repaylText;
	}

	public String getValidateText() {
		return validateText;
	}

	public void setValidateText(String validateText) {
		this.validateText = validateText;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getCommentid() {
		return commentid;
	}

	public void setCommentid(Long commentid) {
		this.commentid = commentid;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public void setReply_content(String replyContent) {
		reply_content = replyContent;
	}

	public String getMsgtitle() {
		return msgtitle;
	}

	public void setMsgtitle(String msgtitle) {
		this.msgtitle = msgtitle;
	}

	public String getMsgcontent() {
		return msgcontent;
	}

	public void setMsgcontent(String msgcontent) {
		this.msgcontent = msgcontent;
	}

	public BeanBaseInfo getBean() {
		return bean;
	}

	public void setBean(BeanBaseInfo bean) {
		this.bean = bean;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getModuletype() {
		return moduletype;
	}

	public void setModuletype(String moduletype) {
		this.moduletype = moduletype;
	}

	public String getModulename() {
		return modulename;
	}

	public void setModulename(String modulename) {
		this.modulename = modulename;
	}

	public String getColumntype() {
		return columntype;
	}

	public void setColumntype(String columntype) {
		this.columntype = columntype;
	}

	public String getColumnname() {
		return columnname;
	}

	public void setColumnname(String columnname) {
		this.columnname = columnname;
	}

	public Map<String, List<BeanBaseInfo>> getInfo() {
		return info;
	}

	public void setInfo(Map<String, List<BeanBaseInfo>> info) {
		this.info = info;
	}

	public Map<String, String> getAdMap() {
		return adMap;
	}

	public void setAdMap(Map<String, String> adMap) {
		this.adMap = adMap;
	}

	public int getClicktime() {
		return clicktime;
	}

	public void setClicktime(int clicktime) {
		this.clicktime = clicktime;
	}

	public BeanUser getUser() {
		return user;
	}

	public void setUser(BeanUser user) {
		this.user = user;
	}

	public Timestamp getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Timestamp createtime) {
		this.createtime = createtime;
	}

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = resource;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Map<String, String> getProp() {
		return prop;
	}

	public void setProp(Map<String, String> prop) {
		this.prop = prop;
	}

	public String getReply_content() {
		return reply_content;
	}

	public void setReplay_content(String replyContent) {
		reply_content = replyContent;
	}

	public String getComment_content() {
		return comment_content;
	}

	public void setComment_content(String commentContent) {
		comment_content = commentContent;
	}

	public Set<BeanInfoComment> getArticleComments() {
		return articleComments;
	}

	public void setArticleComments(Set<BeanInfoComment> articleComments) {
		this.articleComments = articleComments;
	}

	public List<BeanBaseInfo> getInfolist() {
		return infolist;
	}

	public void setInfolist(List<BeanBaseInfo> infolist) {
		this.infolist = infolist;
	}

	public List<BeanBaseInfo> getHotinfolist() {
		return hotinfolist;
	}

	public List<BeanBaseInfo> getRelevanceinfolist() {
		return relevanceinfolist;
	}

	public void setRelevanceinfolist(List<BeanBaseInfo> relevanceinfolist) {
		this.relevanceinfolist = relevanceinfolist;
	}

	public void setHotinfolist(List<BeanBaseInfo> hotinfolist) {
		this.hotinfolist = hotinfolist;
	}

	@Resource
	public void setBeanBaseInfoService(IBeanBaseInfoService beanBaseInfoService) {
		this.beanBaseInfoService = beanBaseInfoService;
	}

	public IBeanADService getAdService() {
		return adService;
	}

	@Resource
	public void setAdService(IBeanADService adService) {
		this.adService = adService;
	}

	// 获取模块信息
	public String findModule() {
		LinkedHashMap<String, String> linkMap = new LinkedHashMap<String, String>();
		linkMap.put("clicktime", "desc");
		BeanBaseInfo beanInfo = new BeanBaseInfo();
		beanInfo.setModuletype(this.getModuletype());
		hotinfolist = beanBaseInfoService.findByExample(beanInfo, linkMap);
		String location = "";
		location = findModuleInfo(this.getModuletype());
		return location;
	}

	// 根据模块 栏目类型获取数据列表
	public String findInfoList() {
		BeanBaseInfo bean = new BeanBaseInfo();
		this.setModuletype(this.getModuletype());
		this.setColumntype(this.getColumntype());
		bean.setModuletype(this.getModuletype());
		bean.setColumntype(this.getColumntype());
		//adMap = ADFileUtil.findCLAD(this.getModuletype() + "-" + this.getColumntype(), "cl");
		infolist = beanBaseInfoService.findByExample(bean);
		Criteria criteria = this.createCriteria(BeanBaseInfo.class).add(Restrictions.eq("moduletype", this.getModuletype())).add(Restrictions.eq("columntype", this.getColumntype())).addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "baseInfoAction!findInfoList.action", 10);
		LinkedHashMap<String, String> linkMap = new LinkedHashMap<String, String>();
		linkMap.put("clicktime", "desc");
		BeanBaseInfo beanInfo = new BeanBaseInfo();
		beanInfo.setModuletype(this.getModuletype());
		hotinfolist = beanBaseInfoService.findByExample(beanInfo, linkMap);
		String location = getLocation(this.getModuletype());
		String specialLocation = getSpecialLocation(this.getModuletype(), this.getColumntype(), "list");
		if (specialLocation != null)
			location = specialLocation;
		return location + "infolist";
	}

	// 获取特殊信息的路径
	private String getSpecialLocation(String mt, String ct, String type) {
		String specialLocation = null;
		if (mt.equals("11")) {
			if (ct.equals("1"))
				specialLocation = "11ex_";
			if (ct.equals("2"))
				specialLocation = "11ar_";
		}
		if (type.equals("content") && mt.equals("3")) {
			if (ct.equals("3"))
				specialLocation = "3ma_";
			if (ct.equals("4"))
				specialLocation = "3fi_";
		}
		if (mt.equals("14")) {
			if (ct.equals("1"))
				specialLocation = "14fu_";
			if (ct.equals("2"))
				specialLocation = "14sp_";
		}
		return specialLocation;
	}

	// 发送短消息
	public String sendMessage() {
		this.setId(this.getId());
		BeanUser ru = this.getBaseService().findById(this.getUserId(), BeanUser.class);
		BeanMessage bean = new BeanMessage();
		bean.setContent(this.getContent());
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		bean.setMsgtype(1);
		// 判断没登陆，提示登陆
		bean.setSender(this.getCurrentUser());
		bean.setReciver(ru);
		this.getBaseService().save(bean);
		return findContent();
	}

	// 回复评论
	public String replyComment() {
		this.setId(this.getId());
		BeanUser user = this.getCurrentUser();
		BeanInfoReply reply = new BeanInfoReply();
		reply.setContent(this.getReply_content());
		reply.setCreatetime(new Timestamp(System.currentTimeMillis()));
		beanBaseInfoService.replyCommment(user.getId(), this.getCommentid(), reply);
		return "redirect_content";
	}

	// 发表评论
	public String saveComment() {
        if(null==this.getCurrentUser()){
        	this.addFieldError("validateTextSUCCESS", "请先登录");
        	return "Comment_Login";
        }else if (StringUtils.isEmpty(validateText)) {
			this.addFieldError("validateTextERROR", "验证码不正确");
		}else if(StringUtils.isEmpty(repaylText)){
			this.addFieldError("validateContentTextERROR", "内容不能为空");
		}else if(validateText.equals(ActionContext.getContext().getSession().get(PublicConfing.MEMBER_LOGIN_RANDOM))){
			bean = beanBaseInfoService.saveComment(this.getId(), this.getRepaylText(),this.getCurrentUser());
			this.setModuletype(bean.getModuletype());
			this.setId(bean.getId());
			this.addFieldError("validateTextSUCCESS", "发表成功");
		}
		return findContent();
	}

	// 获取展示内容
	public String findContent() {
		if (null != this.getId()) {
			// if (this.getId() != Long.parseLong((String)
			// this.getActionContext().getSession().get("BEANBASEINFO_OPTION_ID")))
			// {
			this.getActionContext().getSession().put("BEANBASEINFO_OPTION_ID", this.getId() + "");
			// }
		} else {
			this.setId(Long.parseLong((String) this.getActionContext().getSession().get("BEANBASEINFO_OPTION_ID")));
		}

		LinkedHashMap<String, String> linkMap = new LinkedHashMap<String, String>();
		linkMap.put("createtime", "desc");
		BeanBaseInfo beanInfo = new BeanBaseInfo();
		beanInfo.setModuletype(this.getModuletype());
		relevanceinfolist = beanBaseInfoService.findByExample(beanInfo, linkMap);
		LinkedHashMap<String, String> linkMap2 = new LinkedHashMap<String, String>();
		linkMap.put("clicktime", "desc");
		BeanBaseInfo beanInfo2 = new BeanBaseInfo();
		hotinfolist = beanBaseInfoService.findByExample(beanInfo2, linkMap2);
		bean = beanBaseInfoService.getClickTime(this.getId());
		this.setModuletype(bean.getModuletype());
		String location = getLocation(this.getModuletype());
		String specialLocation = getSpecialLocation(this.getModuletype(), bean.getColumntype(), "content");
		if (specialLocation != null)
			location = specialLocation;

		// 关键字处理
		replaceAllContent();
		infoCommentsList(bean);
		return location + "content";
	}

	/**
	 * 评论回复
	 * 
	 * @return
	 */
	public void infoCommentsList(BeanBaseInfo beanBaseInfo) {
		Criteria criteria = this.createCriteria(BeanInfoComment.class);
		criteria.add(Restrictions.eq("baseinfo", beanBaseInfo));
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		// criteria.add(Restrictions.eq("beanState", 0));
		this.queryByPage(criteria, "baseInfoAction!findContent.action", 10);
	}

	/**
	 * 关键字处理
	 */
	private void replaceAllContent() {
		List<BeanKeywordsFilter> keywords = null;
		keywords = this.getBaseService().findAll(BeanKeywordsFilter.class);
		if (null != keywords && null != bean && StringUtils.isNotEmpty(bean.getContent())) {
			StringBuffer sb = new StringBuffer(bean.getContent());
			for (BeanKeywordsFilter beanKeywordsFilter : keywords) {
				sb = replaceAll(sb, beanKeywordsFilter.getWord(), "?");
			}
			bean.setContent(sb.toString());
		}
	}

	/**
	 * 关键字过滤替换，全部替换
	 * 
	 * @param sb
	 * @param oldStr
	 * @param newStr
	 * @return
	 */
	public static StringBuffer replaceAll(StringBuffer sb, String oldStr, String newStr) {
		int i = sb.indexOf(oldStr);
		int oldLen = oldStr.length();
		int newLen = newStr.length();
		while (i > -1) {
			sb.delete(i, i + oldLen);
			sb.insert(i, newStr);
			i = sb.indexOf(oldStr, i + newLen);
		}
		return sb;
	}

	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("maimmaim");
		System.out.println(replaceAll(sb, "maim", "mai"));
	}

	// 根据模块类型获取栏目分类信息
	private String findModuleInfo(String moduletype) {
		adMap = adService.findAD(moduletype, "md");
		this.setModuletype(moduletype);
		List<BeanModuleColumn> clumns = beanBaseInfoService.getColumns(moduletype);
		for (int i = 0; i < clumns.size(); i++) {
			String columntype = clumns.get(i).getColumntype();
			if (i == 0)
				this.setColumntype(columntype);
			List<BeanBaseInfo> list = beanBaseInfoService.getInfos(moduletype, columntype);
			info.put(columntype, list);
		}
		if (moduletype.equals("3")) {
			List<BeanBaseInfo> list5 = beanBaseInfoService.getInfos("14", "1");
			info.put("5", list5);
			List<BeanBaseInfo> list6 = beanBaseInfoService.getInfos("14", "2");
			info.put("6", list6);
		}
		return getLocation(moduletype);
	}

	// 添加好友
	public String addFriend() {
		this.setId(this.getId());
		BeanUser user = (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long uId = 6L;// user.getId();
		Long fId = this.getUserId();
		beanBaseInfoService.addFriend(uId, fId);
		return findContent();
	}

	// 获取路径
	private String getLocation(String moduletype) {
		int value = Integer.valueOf(moduletype);
		String location = "";
		switch (value) {
		case 1:
			location = "1";
			break;
		case 2:
			location = "2";
			break;
		case 3:
			location = "3";
			break;
		case 4:
			location = "4";
			break;
		case 5:
			location = "5";
			break;
		case 6:
			location = "6";
			break;
		case 7:
			location = "7";
			break;
		case 8:
			location = "8";
			break;
		case 9:
			location = "9";
			break;
		case 10:
			location = "10";
			break;
		case 11:
			location = "11";
			break;
		case 12:
			location = "12";
			break;
		case 13:
			location = "13";
			break;
		default:
			break;
		}
		return location;
	}
}
